﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;

namespace olierFrm.FileServer
{
    /// <summary>
    /// Upload 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class File : System.Web.Services.WebService
    {

        [WebMethod]
        public string Upload(string fileName, byte[] bytes)
        {

            if (System.Configuration.ConfigurationManager.AppSettings["path"] == null)
            {
                throw new Exception("系统配置错误，请配置存储目录！\r\n <add key=\"path\" value=\"upload\"/>");
                //System.Configuration.ConfigurationManager.AppSettings["path"]
            }
            string p = System.Configuration.ConfigurationManager.AppSettings["path"].ToLower();
            var path = Server.MapPath("/") + "\\" + p;
            string date = DateTime.Now.ToString("yyyyMMdd");
            path += "\\" + date;
            string guid = Guid.NewGuid().ToString().Replace("-", "");
            string saveFilePath = path + "\\" + guid;
            if (!System.IO.Directory.Exists(saveFilePath))
            {
                System.IO.Directory.CreateDirectory(saveFilePath);
            }
            FileStream fStream = new FileStream(saveFilePath + "\\" + fileName, FileMode.Create, FileAccess.Write);
            fStream.Write(bytes, 0, bytes.Length);
            fStream.Close();

            string url = "http://" + HttpContext.Current.Request.Url.Host + "/" + p + "/" + date + "/" + guid + "/" + fileName;

            return url;
        }
    }
}
